package com.art.sunflower.service.impl;

import com.art.sunflower.enumeration.RoleType;
import com.art.sunflower.exception.NullTeacherException;
import com.art.sunflower.mapper.TeacherMapper;
import com.art.sunflower.mapper.UserMapper;
import com.art.sunflower.pojo.Teacher;
import com.art.sunflower.service.TeacherService;
import com.art.sunflower.util.Md5SaltTool;
import com.art.sunflower.util.MyPageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {

    @Resource
    TeacherMapper teacherMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public MyPageInfo getList(Integer pageSize, Integer pageNo) throws NullTeacherException {
        PageHelper.startPage(pageNo, pageSize);
        List<Map<String, Object>> teachers = teacherMapper.selectList();
        if (teachers.size() == 0)
            throw new NullTeacherException();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(teachers);
        return new MyPageInfo(pageInfo);
    }

    @Override
    public void add(String username, String password, String email, String phone, String name) throws NoSuchAlgorithmException {
        userMapper.insertUserAll(username, Md5SaltTool.getEncryptedPwd(password), RoleType.teacher, name, phone);
        Integer id = userMapper.selectLastInsertId();
        teacherMapper.insertTeacher(id, email);
    }

    @Override
    public void update(Integer id, String username, String password, String email, String phone, String name) throws NoSuchAlgorithmException, NullTeacherException {
        Map<String, Object> teacher = teacherMapper.selectTeacher(id);
        if (teacher == null) {
            throw new NullTeacherException();
        } else {
            userMapper.updateUser((Integer) teacher.get("user_id"), username, Md5SaltTool.getEncryptedPwd(password), name, phone);
            teacherMapper.updateEmail(id, email);
        }
    }

    @Override
    public void delete(Integer id) throws NullTeacherException {
        Map<String, Object> teacher = teacherMapper.selectTeacher(id);
        if (teacher == null) {
            throw new NullTeacherException();
        } else {
            teacherMapper.delete(id);
        }
    }

    @Override
    public Map<String, Object> getById(Integer id) throws NullTeacherException {
        Map<String, Object> teacher = teacherMapper.selectTeacher(id);
        if (teacher == null) {
            throw new NullTeacherException();
        } else {
            return teacherMapper.selectById(id);
        }
    }

    @Override
    public Teacher selectOneTeacherById(Integer id) {
        return teacherMapper.selectOneTeacherById(id);
    }

    @Override
    public List<Map<String, Object>> getNameList() throws NullTeacherException {
        List<Map<String, Object>> teachers = teacherMapper.selectName();
        if (teachers.size() == 0)
            throw new NullTeacherException();
        return teachers;
    }

    @Override
    public void updateStatus(Integer id) throws NullTeacherException {
        Teacher teacher = teacherMapper.selectOneTeacherById(id);
        if (teacher == null) {
            throw new NullTeacherException();
        } else {
            teacherMapper.updateStatus(id, !teacher.getIs_leave());
        }
    }

}
